package com.neusoft.databus.policy.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.databus.policy.dto.ExpressionDTO;
import com.neusoft.databus.policy.model.Expression;
import com.neusoft.databus.policy.repository.ExpressionRepository;

/**
 * 策略表达式Converter
 * 
 * @author naxiang
 */
@Component
public class ExpressionConverter extends AbstractConverter<Expression, ExpressionDTO> {
    @Autowired
    private ExpressionRepository expressionRepository;

    @Override
    public Expression toModel(ExpressionDTO dto) {
        Expression model;
        if (dto.isNew()) {
            model = new Expression();
        } else {
            model = this.expressionRepository.getOne(dto.getId());
        }
        model.setType(dto.getType());
        model.setExpressionInfo(dto.getExpressionInfo());
        model.setLinkSymbol(dto.getLinkSymbol());
        model.setSequence(dto.getSequence());
        model.setExtendProperty(dto.getExtendProperty());
        return model;
    }

    @Override
    public ExpressionDTO toDTO(Expression model, boolean forListView) {
        ExpressionDTO dto = new ExpressionDTO();
        dto.setId(model.getId());
        dto.setType(model.getType());
        dto.setExpressionInfo(model.getExpressionInfo());
        dto.setLinkSymbol(model.getLinkSymbol());
        dto.setSequence(model.getSequence());
        dto.setExtendProperty(model.getExtendProperty());
        return dto;
    }

}
